<?php
// The source code packaged with this file is Free Software, Copyright (C) 2005 by
// Ricardo Galli <gallir at uib dot es>.
// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
// You can get copies of the licenses here:
// 		http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".

ini_set('include_path', '.');

//error_reporting(E_ALL);


// experimental caching
// 0 = off
// 1 = on
define('caching', 1);

define('summarize_mysql', 1);










// DO NOT EDIT THIS FILE. USE THE ADMIN PANEL (logged in as "god") TO MAKE CHANGES


// IF YOU MUST MAKE CHANGES MANUALLY, EDIT SETTINGS.PHP










if (strpos($_SERVER['SCRIPT_NAME'], "install.php") == 0){
	$file = dirname(__FILE__) . '/settings.php';
	if (!file_exists($file)) { $errors[]="'$file' was not found! Try renaming 'settings.php.default' to 'settings.php'"; }
	elseif (filesize($file) <= 0) { $errors[]="'$file' is 0 bytes!"; }
	
	$file = dirname(__FILE__) . '/libs/dbconnect.php';
	if (!file_exists($file)) { $errors[]="'$file' was not found! Try renaming 'dbconnect.php.default' to 'dbconnect.php'"; }
	
	$file= dirname(__FILE__) . '/templates_c';
	if (!file_exists($file)) { $errors[]="'$file' was not found! Create a directory called templates_c in your root directory."; }
	elseif (!is_writable($file)) { $errors[]="'$file' is not writable! Please chmod this directory to 777"; }
	
	$file= dirname(__FILE__) . '/cache';
	if (!file_exists($file)) { $errors[]="'$file' was not found! Create a directory called cache in your root directory."; }
	elseif (!is_writable($file)) { $errors[]="'$file' is not writable! Please chmod this directory to 777"; }
	
	if (isset($errors)) {
		foreach ($errors as $error) {
			$output.="<p><b>Error:</b> $error</p>\n";
			$output.='<p>Please fix the above error(s), install halted!</p>';
		}
		die($output);
	}
}

define("mnmpath", dirname(__FILE__).'/');
define("mnminclude", dirname(__FILE__).'/libs/');
define("mnmmodules", dirname(__FILE__).'/modules/');

include_once 'settings.php';
if ($my_base_url == '' || strpos($_SERVER['SCRIPT_NAME'], "admin_config.php") > 0){
	define('my_base_url', "http://" . $_SERVER["HTTP_HOST"]);
	if(isset($_REQUEST['action'])){$action = $_REQUEST['action'];}else{$action="";}
	
	$pos = strrpos($_SERVER["SCRIPT_NAME"], "/");
	$path = substr($_SERVER["SCRIPT_NAME"], 0, $pos);
	if ($path == "/"){$path = "";}

	define('my_pligg_base', $path);
	$my_pligg_base = $path;
} else {
	define('my_base_url', $my_base_url);
	define('my_pligg_base', $my_pligg_base);
}

define('urlmethod', $URLMethod);

if(isset($_COOKIE['template'])){
	$thetemp = $_COOKIE['template'];
} 

// template check
$file = dirname(__FILE__) . '/templates/' . $thetemp . "/pligg.tpl";
if (!file_exists($file)) { $errors[]='You may have typed the template name wrong or "'. $thetemp . '" does not exist. Click <a href = "admin_config.php?page=Template">here</a> to fix it.'; }
if (isset($errors)) {
	$thetemp = "yget";
	$file = dirname(__FILE__) . '/templates/' . $thetemp . "/pligg.tpl";
	if (!file_exists($file)) {echo 'The default yget template does not exist anymore. Please fix this so you can fix other problems!'; die();}

	foreach ($errors as $error) {
		$output.="<p><b>Error:</b> $error</p>\n";
	}

	if (strpos($_SERVER['SCRIPT_NAME'], "admin_config.php") == 0 && strpos($_SERVER['SCRIPT_NAME'], "login.php") == 0){
		echo $output;
 		die();
	}
}


define('The_Template', $thetemp);

if(Enable_Extra_Fields){include mnminclude.'extra_fields.php';}

// Don't touch behind this
$local_configuration = $_SERVER['SERVER_NAME'].'-local.php';
@include($local_configuration);

include_once mnminclude.'define_tables.php';

//
// start summarization and caching of mysql data
//

	// added to replace 55 redundant queries with 1
	// used with the following functions in /lib/link.php
	//	function category_name() {
	//	function category_safe_name() {
	$the_cats = $db->get_results("select category_id, category_name, category_safe_name, category_lang from ".table_categories.";");
	
	// a simple cache type system for the users table
	// used in the read() function of /libs/user.php
	$cached_users = array();
	
	// a simple cache type system for the totals table
	// functions related to this are in /libs/html1.php	
	$cached_totals = array();

	$cached_votes = array();

	$cached_links = array();
//
// end summarization and caching of mysql data
//

ob_start();
include_once mnminclude.'db.php';
include mnminclude.'utils.php';
include_once mnminclude.'login.php';
include_once(mnmmodules . 'modules_init.php');
include mnminclude.'utf8/utf8.php';
include_once(mnminclude.'dbtree.php');
?>